<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="0px" class="top-left">
      <el-form-item label prop="guigxh">
        <el-input v-model="queryParams.guigxh" placeholder="请输入规格型号" clearable @keyup.enter.native="handleQuery" style="width: 160px" />
      </el-form-item>
      <el-form-item label prop="supId">
        <el-select v-model="queryParams.supId" filterable placeholder="请选择供应商">
          <el-option v-for="item in supList" :key="item.supId" :label="item.supName" :value="item.supId"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label prop="inDate">
        <el-date-picker clearable v-model="queryParams.inDate" type="date" value-format="yyyy-MM-dd" placeholder="请选择进场日期" style="width: 160px"></el-date-picker>
      </el-form-item>
      <el-form-item label prop="cunfwz">
        <el-input v-model="queryParams.cunfwz" placeholder="请输入存放位置" clearable @keyup.enter.native="handleQuery" style="width: 160px" />
      </el-form-item>
      <!-- <el-form-item label prop="pih">
        <el-input v-model="queryParams.pih" placeholder="请输入批号" clearable @keyup.enter.native="handleQuery"
          style="width: 160px;" />
      </el-form-item> -->
      <el-form-item prop="clType">
        <el-select v-model="queryParams.clType" filterable placeholder="请选择材料类型">
          <el-option v-for="dict in dict.type.cl_type" :key="dict.value" :label="dict.label" :value="dict.value"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label prop="chucDate">
        <el-date-picker clearable v-model="queryParams.chucDate" type="date" value-format="yyyy-MM-dd" placeholder="请选择出厂日期" style="width: 160px"></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb5 top-right-btn small-line-height">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd" v-hasPermi="['ycl:bsJctz:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate" v-hasPermi="['ycl:bsJctz:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete" v-hasPermi="['ycl:bsJctz:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport" v-hasPermi="['ycl:bsJctz:export']">导出</el-button>
      </el-col>
    </el-row>

    <el-table border stripe v-loading="loading" :data="bsJctzList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="试验室" align="center" prop="deptName" min-width="120" show-overflow-tooltip />
      <el-table-column label="材料类型" align="center" prop="clType" min-width="120">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.cl_type" :value="scope.row.clType" />
        </template>
      </el-table-column>
      <el-table-column label="规格型号" align="center" prop="guigxh" min-width="120" show-overflow-tooltip />
      <el-table-column label="供应商" align="center" prop="supName" min-width="200" show-overflow-tooltip />
      <el-table-column label="产地厂家" align="center" prop="chandcj" min-width="200" show-overflow-tooltip />
      <el-table-column label="进场数量" align="center" prop="jincsl" min-width="120" show-overflow-tooltip />
      <el-table-column label="进场日期" align="center" prop="inDate" width="120">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.inDate, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column label="存放位置" align="center" prop="cunfwz" min-width="120" show-overflow-tooltip />
      <el-table-column label="批号" align="center" prop="pih" min-width="120" show-overflow-tooltip />
      <el-table-column label="出厂日期" align="center" prop="chucDate" width="120">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.chucDate, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <!--      <el-table-column label="代表数量" align="center" prop="daibsl" min-width="120" show-overflow-tooltip />-->
      <el-table-column label="被试验次数" align="center" prop="syTimes" min-width="100" show-overflow-tooltip />
      <el-table-column label="操作" align="center" fixed="right" class-name="small-padding fixed-width" width="160">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)" v-hasPermi="['ycl:bsJctz:edit']">修改</el-button>
          <el-button size="mini" type="text" @click="onUploadFile(scope.row)" v-hasPermi="['ycl:bsJctz:edit']"><span class="iconfont icon-shangchuanfujian"></span>附件</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)" v-hasPermi="['ycl:bsJctz:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList" />

    <!-- 添加或修改原材料进场台账对话框 -->
    <jy-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="材料类型" prop="clType">
              <el-select v-model="form.clType" placeholder="请选择材料类型" style="width: 100%">
                <el-option v-for="dict in dict.type.cl_type" :key="dict.value" :label="dict.label" :value="dict.value"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="规格型号" prop="guigxh">
              <el-input v-model="form.guigxh" placeholder="请输入规格型号" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="供应商" prop="supId">
              <el-select v-model="form.supId" filterable placeholder="请选择供应商" style="width: 100%">
                <el-option v-for="item in supList" :key="item.supId" :label="item.supName" :value="item.supId"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="产地厂家" prop="chandcj">
              <el-input v-model="form.chandcj" placeholder="请输入产地厂家" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="进场数量" prop="inNum">
              <el-input type="number" v-model="form.inNum" placeholder="请输入进场数量">
                <el-select v-model="form.inUnit" slot="append" placeholder="请选择" style="width: 100px">
                  <el-option v-for="dict in dict.type.unit" :key="dict.value" :label="dict.label" :value="dict.value"></el-option>
                </el-select>
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="进场日期" prop="inDate">
              <el-date-picker clearable v-model="form.inDate" type="date" value-format="yyyy-MM-dd" placeholder="请选择进场日期" style="width: 100%"></el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="存放位置" prop="cunfwz">
              <el-input v-model="form.cunfwz" placeholder="请输入存放位置" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="批号" prop="pih">
              <el-input v-model="form.pih" placeholder="请输入批号" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="出厂日期" prop="chucDate">
              <el-date-picker clearable v-model="form.chucDate" type="date" value-format="yyyy-MM-dd" placeholder="请选择出厂日期" style="width: 100%"></el-date-picker>
            </el-form-item>
          </el-col>
          <!--          <el-col :span="12">-->
          <!--            <el-form-item label="代表数量" prop="daibNum">-->
          <!--              <el-input type="number" v-model="form.daibNum" placeholder="请输入代表数量">-->
          <!--                <el-select v-model="form.daibUnit" slot="append" placeholder="请选择" style="width: 100px;">-->
          <!--                  <el-option v-for="dict in dict.type.unit" :key="dict.value" :label="dict.label" :value="dict.value"></el-option>-->
          <!--                </el-select>-->
          <!--              </el-input>-->
          <!--            </el-form-item>-->
          <!--          </el-col>-->
          <el-col :span="24">
            <el-form-item label="备注" prop="tzRemark">
              <el-input type="textarea" v-model="form.tzRemark" placeholder="请输入备注" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="small" :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
        <el-button size="small" @click="cancel">取 消</el-button>
      </div>
    </jy-dialog>
    <file-upload-view ref="fileUploadViewRef" :moduleKey="'jctz'" :file-size="20" />
  </div>
</template>

<script>
import { listBsJctz, getBsJctz, delBsJctz, addBsJctz, updateBsJctz } from "@/api/ycl/bsJctz";
import { listBsSup } from "@/api/ycl/bsSup";
import { mapGetters } from "vuex";
import FileUploadView from "@/components/FileUpload/FileUploadView.vue";

export default {
  name: "BsJctz",
  dicts: ["cl_type", "unit"],
  components: {
    FileUploadView
  },
  data() {
    return {
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 原材料进场台账表格数据
      bsJctzList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        deptId: undefined,
        guigxh: undefined,
        supId: undefined,
        inDate: undefined,
        cunfwz: undefined,
        pih: undefined,
        chucDate: undefined,
        tzStatus: undefined
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
      // 供应商列表
      supList: []
    };
  },
  // 监听顶部全局下拉
  computed: {
    ...mapGetters(["deptId"])
  },
  watch: {
    deptId(newValue, oldValue) {
      this.queryParams.deptId = newValue;
      this.getList();
    }
  },
  created() {
    this.queryParams.deptId = this.$store.state.dept.deptId;
    this.getList();
    this.getSupList();
  },
  methods: {
    /** 查询供应商列表 */
    getSupList() {
      let param = {
        pageNum: 1,
        pageSize: 9999
      };
      listBsSup(param).then((response) => {
        this.supList = response.rows;
      });
    },
    /** 查询原材料进场台账列表 */
    getList() {
      this.loading = true;
      listBsJctz(this.queryParams).then((response) => {
        this.bsJctzList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        tzId: undefined,
        deptId: undefined,
        clType: undefined,
        guigxh: undefined,
        supId: undefined,
        chandcj: undefined,
        inNum: undefined,
        inUnit: undefined,
        jincsl: undefined,
        inDate: undefined,
        jincrq: undefined,
        cunfwz: undefined,
        pih: undefined,
        chucDate: undefined,
        chucrq: undefined,
        daibNum: undefined,
        daibUnit: undefined,
        daibsl: undefined,
        syTimes: undefined,
        tzStatus: undefined,
        tzRemark: undefined,
        delFlag: undefined,
        createBy: undefined,
        createTime: undefined,
        updateBy: undefined,
        updateTime: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.tzId);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加原材料进场台账";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.loading = true;
      this.reset();
      const tzId = row.tzId || this.ids;
      getBsJctz(tzId).then((response) => {
        this.loading = false;
        this.form = response.data;
        this.open = true;
        this.title = "修改原材料进场台账";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          this.buttonLoading = true;
          if (this.form.tzId != null) {
            updateBsJctz(this.form)
              .then((response) => {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              })
              .finally(() => {
                this.buttonLoading = false;
              });
          } else {
            // 默认当前试验室
            this.form.deptId = this.queryParams.deptId;
            addBsJctz(this.form)
              .then((response) => {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              })
              .finally(() => {
                this.buttonLoading = false;
              });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const tzIds = row.tzId || this.ids;
      this.$modal
        .confirm('是否确认删除原材料进场台账编号为"' + tzIds + '"的数据项？')
        .then(() => {
          this.loading = true;
          return delBsJctz(tzIds);
        })
        .then(() => {
          this.loading = false;
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {})
        .finally(() => {
          this.loading = false;
        });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "ycl/bsJctz/export",
        {
          ...this.queryParams
        },
        `bsJctz_${new Date().getTime()}.xlsx`
      );
    },

    // 附件上传
    onUploadFile(row) {
      this.$refs.fileUploadViewRef.init(row.tzId);
    }
  }
};
</script>
